﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Application.DomainObjects;
using ROS.Persistence.DAO;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence
{
    internal class MenuCategoryDataMapper : IDataMapper<MenuCategory>
    {
        #region Fields

        private IMenuCategoryDAO _menuCategoryDAO;

        #endregion

        #region Properties

        public IMenuCategoryDAO menuCategoryDAO
        {
            get
            {
                if (_menuCategoryDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    _menuCategoryDAO = daoFactory.GetMenuCategoryDAO();
                }
                return _menuCategoryDAO;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new MenuCategoryDataMapper object.
        /// </summary>
        public MenuCategoryDataMapper()        
        {
            _menuCategoryDAO = null;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Insert a menu category into the data source.
        /// </summary>
        /// <param name="menuItem"></param>
        /// <returns></returns>
        public void Create(MenuCategory menuCategory)
        {
            MenuCategoryDTO menuCategoryDTO = new MenuCategoryDTO();
            menuCategoryDTO.Name = menuCategory.Name;
            menuCategoryDAO.Insert(menuCategoryDTO);
        }

        public MenuCategory FindByID(long id)
        {
            return LoadMenuCategory(menuCategoryDAO.SelectByID(id));
        }

        public IList<MenuCategory> FindAll()
        {
            IList<MenuCategory> menuCategoryList = new List<MenuCategory>();
            IList<MenuCategoryDTO> menuCategoryDTOList = menuCategoryDAO.SelectAll();
            foreach (MenuCategoryDTO menuCategoryDTO in menuCategoryDTOList)
                menuCategoryList.Add(LoadMenuCategory(menuCategoryDTO));
            return menuCategoryList;
        }

        /// <summary>
        /// Delete a menu category by its ID.
        /// </summary>
        /// <param name="menuCategoryID"></param>
        internal void Delete(long menuCategoryID)
        {
            menuCategoryDAO.DeleteByID(menuCategoryID);
        }

        /// <summary>
        /// Load a menu category from a menu category DTO.
        /// </summary>
        /// <param name="menuCategoryDTO"></param>
        /// <returns></returns>
        private MenuCategory LoadMenuCategory(MenuCategoryDTO menuCategoryDTO)
        {
            return new MenuCategory(menuCategoryDTO.ID,
                                    menuCategoryDTO.Name);
        }

        #endregion

        public void Update(MenuCategory domainObject)
        {
            throw new NotImplementedException();
        }

        public void Delete(MenuCategory domainObject)
        {
            throw new NotImplementedException();
        }
    }
}
